package com.tanhua.server.contoller;

import com.tan.model.domain.UserInfo;
import com.tan.model.vo.CountVo;
import com.tan.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.Userthread;
import com.tanhua.server.service.UserInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UserInfoService userInfoService;
    @GetMapping
    public ResponseEntity getUsersInfo(@RequestHeader("Authorization")String token, Long userID) {
        UserInfoVo userInfoVo = new UserInfoVo();
//      Claims claims =  JwtUtils.verify(token);
//     Integer id = (Integer) claims.get("id");
        Long id = Userthread.getUser().getId();
     if (userID==null){
         userID=id;
     }
     UserInfo userInfo = userInfoService.getUserInfo(userID);
     BeanUtils.copyProperties(userInfo,userInfoVo);//复制 同名同类型属性
        userInfoVo.setAge(userInfo.getAge().toString());
     return ResponseEntity.ok(userInfoVo);

    }
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo,@RequestHeader("Authorization")String token) {
//        Claims claims =  JwtUtils.verify(token);
//       Integer integer =(Integer) claims.get("id");
            Long id = Userthread.getUser().getId();
       userInfoService.updateUserInfo(userInfo,id);
       return ResponseEntity.ok(null);
    }
    @PostMapping("/header")
    public ResponseEntity updateUserInfo(MultipartFile headPhoto, @RequestHeader("Authorization")String token) throws IOException {
        Long id = Userthread.getUser().getId();
        System.out.println(id);
        userInfoService.update(headPhoto,id);
        return ResponseEntity.ok(null);

    }
    @GetMapping("/counts")
    public ResponseEntity getCounts() {
      CountVo countVo = userInfoService.getCounts();
       return ResponseEntity.ok(countVo);

    }
}
